package com.sgb.kjwl.view.ui.fragment.packers;

import android.content.Intent;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;


import com.sgb.kjwl.AppConstant;
import com.sgb.kjwl.AppLazyFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FragmentShopTdChildCommonBinding;
import com.sgb.kjwl.di.packers.DaggerPackersFragmentComponent;
import com.sgb.kjwl.di.packers.PackersFragmentModule;
import com.sgb.kjwl.model.entity.ShopTeamInfoEntity;
import com.sgb.kjwl.model.entity.self.ResponseList;
import com.sgb.kjwl.utils.DisplayUtil;
import com.sgb.kjwl.view.ui.activity.PhotoViewActivity;
import com.sgb.kjwl.view.ui.activity.packers.TeamOfferDetailActivity;
import com.sgb.kjwl.view.ui.adapter.shopping.ShopTeamAdapter;
import com.sgb.kjwl.viewmodel.self.packers.PersonCommonModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.util.ScreenUtil;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

/**
 *
 * @author WYJ
 * @date 2020/8/31
 * 企业店铺 工队简介
 */
public class PersonCommonFragment extends AppLazyFragment {
    public static final String SHOP_ID = "shop_id";
    public static final String SHOP_NO = "shop_no";
    public static final String CHANNEL_ID = "channel_id";
    @Inject
    PersonCommonModel mModel;
    private FragmentShopTdChildCommonBinding mBind;
    private ShopTeamAdapter mAdapter;

    private String mShopId;
    private String mChannelId;
    private String mShopNo;
    private List<String> listimage;

    public static PersonCommonFragment newInstance(String shopId,String NO, String id) {
        PersonCommonFragment fragment = new PersonCommonFragment();
        Bundle bundle = new Bundle();
        bundle.putString(SHOP_ID, shopId);
        bundle.putString(SHOP_NO, NO);
        bundle.putString(CHANNEL_ID, id);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBind = DataBindingUtil.inflate(inflater, R.layout.fragment_shop_td_child_common, container, false);
        return mBind.getRoot();
    }

    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        DaggerPackersFragmentComponent
                .builder()
                .appComponent(appComponent)
                .packersFragmentModule(new PackersFragmentModule(this))
                .build()
                .inject(this);
    }


    @Override
    protected void initView() {
        mShopId = getArguments().getString(SHOP_ID);
        mShopNo = getArguments().getString(SHOP_NO);
        mChannelId = getArguments().getString(CHANNEL_ID);
        mBind.setViewModel(null);
        // teamInfo:工队简介
        // teamPublish:我的发布
        // teamOffer:工种报价
        // teamAchieve:施工业绩
        // teamMember:工队人员
        // teamEquipment:自有设备
        int layoutId = 0;
        assert getArguments() != null;
        switch (mChannelId) {
            case "teamPublish": //我的发布
                mBind.teamIntroduction.setVisibility(View.GONE);
                layoutId = R.layout.item_shop_team_pulish;
                break;
            case "teamOffer": //工种报价
                mBind.teamIntroduction.setVisibility(View.VISIBLE);
                layoutId = R.layout.item_shop_team_offer;
                break;
            case "teamAchieve": //施工业绩
                mBind.teamIntroduction.setVisibility(View.VISIBLE);
                layoutId = R.layout.item_shop_team_achieve;
                break;
            case "teamMember":  //工队人员
                mBind.teamIntroduction.setVisibility(View.VISIBLE);
                layoutId = R.layout.item_shop_team_member;
                break;
            case "teamEquipment":  //自有设备
                mBind.teamIntroduction.setVisibility(View.VISIBLE);
                layoutId = R.layout.item_shop_team_device;
                break;
            case "teamAlbum":  //工队相册
                mBind.teamIntroduction.setVisibility(View.GONE);
                layoutId = R.layout.item_shop_team_album;
                break;
            default:
                return;
        }
        mAdapter = new ShopTeamAdapter(getActivity(), layoutId, new ArrayList<>());
        mAdapter.setChannelId(mChannelId);
        if (mChannelId.equals("teamAlbum")){
            mBind.rvX.setLayoutManager(new GridLayoutManager(getActivity(),3));
            mBind.rvX.setPadding(0,14,0,20);
            mBind.rvX.setBackgroundColor(getActivity().getResources().getColor(R.color.white));
            mBind.rvX.addItemDecoration(new RecyclerView.ItemDecoration() {
                @Override
                public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
                    super.getItemOffsets(outRect, view, parent, state);
                    //不是第一个的格子都设一个左边和底部的间距
                    outRect.left = ScreenUtil.dip2px(4);
                    outRect.right = ScreenUtil.dip2px(4);
                    outRect.bottom = ScreenUtil.dip2px(5);
                    //由于每行都只有3个，所以第一个都是3的倍数，把左边距设为0
                    int position = parent.getChildLayoutPosition(view);
                    if (position % 3 == 0) {
                        outRect.right = ScreenUtil.dip2px(15);

                    }
                    if (position % 3 == 1) {
                        outRect.left = ScreenUtil.dip2px(15);
                    }
                }
            });
        }else {
            mBind.rvX.setLayoutManager(new LinearLayoutManager(getActivity()));
        }
        mBind.rvX.setAdapter(mAdapter);
        mAdapter.setOnItemClick(new AdapterOnItemClick<ShopTeamInfoEntity>() {
            @Override
            public void onItemClick(ShopTeamInfoEntity o, int position) {
                switch (mChannelId) {
                    case "teamPublish": //我的发布
                        mModel.getH5Url(o.getNo(), "ProjectDetails", -1);
                        break;
                    case "teamOffer": //工种报价
                        TeamOfferDetailActivity.start(getActivity(), o.getNo());
                        break;
                    case "teamAchieve": //施工业绩
                        mModel.getH5Url(o.getNo(), "constructionAchievementsDetails", -1);
                        break;
                    case "teamMember":  //工队人员
                        mModel.getH5Url(o.getNo(), "teamMembersDetails", -1);
                        break;
                    case "teamEquipment":  //自有设备
                        mModel.getH5Url(o.getNo(), "groupEquipmentDeatil", -1);
                        break;
                    case "teamAlbum":  //工队相册
                        if (listimage!=null&&listimage.size()>0){
                            Intent intent = new Intent();
                            intent.setClass(getActivity(), PhotoViewActivity.class);
                            intent.putExtra(PhotoViewActivity.KEY_PHOTO_URLS, (Serializable) listimage);
                            intent.putExtra(PhotoViewActivity.KEY_PHOTO_POSITION, position);
                            getActivity().startActivity(intent);
                        }
                        break;
                    default:
                        return;
                }
            }
        });
        mBind.rvX.setLoadingMoreEnabled(true);
        mBind.rvX.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                if ("teamMember".equals(mChannelId)|| "teamAlbum".equals(mChannelId)|| "teamOffer".equals(mChannelId)|| "teamAchieve".equals(mChannelId)|| "teamEquipment".equals(mChannelId)){
                    mModel.getTuanDuiData(true, mShopId, mChannelId);
                }else {
                    mModel.getData(true, mShopId, mChannelId);
                }
            }

            @Override
            public void onLoadMore() {
                if ("teamMember".equals(mChannelId)|| "teamAlbum".equals(mChannelId)|| "teamOffer".equals(mChannelId)|| "teamAchieve".equals(mChannelId)|| "teamEquipment".equals(mChannelId)){
                    mModel.getTuanDuiData(false, mShopId, mChannelId);
                }else {
                    mModel.getData(false, mShopId, mChannelId);
                }
            }
        });
    }

    @Override
    protected void onLazyLoad() {
        if (mChannelId.equals("teamMember")||mChannelId.equals("teamAlbum")||mChannelId.equals("teamOffer")||mChannelId.equals("teamAchieve")||mChannelId.equals("teamEquipment")){
            mModel.getTuanDuiData(true, mShopId, mChannelId);
        }else {
            mModel.getData(true, mShopId, mChannelId);
        }
    }

    public void showList(List<ShopTeamInfoEntity> data, int curPager) {
        mBind.rvX.refreshComplete();
        mBind.rvX.loadMoreComplete();
        DialogHelper.dismissProgressDialog();
        if (curPager == 1) {
            if (data == null || data.size() == 0) {
                showEmpty();
                return;
            }
            mAdapter.setDatas(data);
        } else {
            if (data != null) {
                mAdapter.addDatas(data);
            }
        }
        mBind.rvX.setLoadingMoreEnabled(mAdapter.getItemCount() >= curPager * AppConstant.PAGER_LIMIT);
    }

    public void showEmpty() {
        mBind.rvX.refreshComplete();
        mBind.rvX.loadMoreComplete();
        DialogHelper.dismissProgressDialog();
        mBind.emptyView.setVisibility(View.VISIBLE);
    }

    public void showTuanduiList(ResponseList<List<ShopTeamInfoEntity>> data, int curPager) {
        if (data==null) {
            return;
        }
        List<ShopTeamInfoEntity> list = data.getList();
        listimage = new ArrayList<>();
        for (ShopTeamInfoEntity shopTeamInfoEntity : list) {
            listimage.add(shopTeamInfoEntity.getUrl());
        }
        mBind.rvX.refreshComplete();
        mBind.rvX.loadMoreComplete();
        DialogHelper.dismissProgressDialog();
        if (mChannelId.equals("teamMember")){
            mBind.teamIntroductionNum.setText("团队人数 "+data.getTotalRow()+" 人");
        }
        if (mChannelId.equals("teamOffer")){
            mBind.teamIntroductionNum.setText("工种报价 "+data.getTotalRow()+" 项");
        }
        if (mChannelId.equals("teamAchieve")){
            mBind.teamIntroductionNum.setText("工程业绩 "+data.getTotalRow()+" 项");
        }
        if (mChannelId.equals("teamEquipment")){
            mBind.teamIntroductionNum.setText("专业设备 "+data.getTotalRow()+" 台");
        }
        if (curPager == 1) {
            if (list == null || list.size() == 0) {
                showEmpty();
                return;
            }
            mAdapter.setDatas(list);
        } else {
            if (data != null) {
                mAdapter.addDatas(list);
            }
        }
        mBind.rvX.setLoadingMoreEnabled(mAdapter.getItemCount() >= curPager * AppConstant.PAGER_LIMIT);
    }
}
